package org.istt.web;

import org.istt.domain.Position;
import org.istt.service.PositionManager;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 *
 */
public class PositionAction extends BaseAction<PositionManager, Position, Long> {
	private Position position;
	private Long id;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	
	public void setId(Long id) {
		this.id = id;
	}
	@Override
	public Long getId() {
/*		Long id = NumberUtils.toLong(position.getId());
		if (id == 0) return null;
		return id;*/
		return (position==null? id : position.getId());//id
	}


	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("position.saved"));
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("position.deleted"));
	}


	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }
        
        //entity = getEntity();
        ((PositionManager)mgr).save(position);
        
        setSaveMessage();
                
        return SUCCESS;
	}
	
	public boolean isValid() {
		return mgr.isUnique(new String[]{"name"}, new String[]{position.getName()}, position.getId());
	}
	
	//Added by NDTHO.2011.12.09
	@Override
	public void validate() {
		if (!isValid()) {// create new entity
				addActionError(getText("position.name.existed"));
		} 
	}

    
	public void setPosition(Position position) {
		this.position = position;
	}

	public Position getPosition() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return position;
	}

}
